<%@page import="multiface.awwadco.jpa.Utente"%>
<%@page import="multiface.web.util.Util"%>
<%@page import="multiface.web.util.Paginator"%>
<%
    Utente ut = (Utente) session.getAttribute("admin");
    if (ut == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>
<%@page import="multiface.web.util.PagerList"%>
<%@page import="javax.persistence.Query"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Iterator"%>
<%@page import="multiface.awwadco.jpa.Prodotto"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.RollbackException"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="multiface.awwadco.jpa.Categoria"%>
<%@page import="multiface.awwadco.jpa.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="multiface.awwadco.jpa.PersistenceManager"%>
<div class="box">

    <%
        // PRODOTTI DEL RICAMBIO
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();

        //Parametri della jsp
        String order = request.getParameter("order");
        String direction = request.getParameter("direction");
        String text = request.getParameter("text");
        String jsp = "view_prod.jsp";
        String functionJS = "open";
        String indexs = request.getParameter("index");
        int index = 0;
        int pid = Integer.parseInt(request.getParameter("pid"));
        int part_id = pid;
        
        //Controlli sui parametri
        if (indexs != null)index = Integer.parseInt(indexs);
        if(direction==null)direction="ASC";
        
        //Paginator
        Paginator paginator = new Paginator();
        paginator.setCSSClass("pager-list");
        paginator.setCSSClassCurrentPage("selected");
        paginator.setCurrentIndex(index);
        paginator.setElementsForPage_fromDB(true);
        paginator.setFunctionJS(functionJS);
        paginator.setPageReturn(jsp);
        
        //Set Parametri iniziali
        paginator.addParameter("index", indexs);

        //Ricambio selezionato
        Prodotto prod = em.find(Prodotto.class, Integer.parseInt(request.getParameter("pid")));
        em.refresh(prod);
        out.print("<div class=\"title\">Products of : " + prod.getNome() + "</div><br>");
        
        
        //Lista de Prodotti del ricambio
        List<Prodotto> list = prod.getProdottoList();

        //Iterazione della tabella
        Iterator i = list.iterator();
        out.print("<table id=\"table-view-products\" class=\"listing\" border=\"0\">");
        out.print("<tr>");
        out.print("<td>"+paginator.getFunction("order", "codice", "Code") +"</td>");
        out.print("<td>"+paginator.getFunction("order", "nome", "Name") +"</td>");
        out.print("<td>"+paginator.getFunction("order", "anno", "Year") +"</td>");
        out.print("<td>"+paginator.getFunction("order", "dataora", "Date") +"</td>");
        out.print("<td colspan=\"4\">Fast Setting</td>");
        out.print("</tr>");
        while (i.hasNext()) {
            Prodotto p2 = (Prodotto) i.next();
            
            int prod_id = p2.getIdprodotto();

            String img_promo = "notpromo.png";
            if (p2.getOfferta()) {
                img_promo = "promo.png";
            }
            String img_home = "nothome.png";
            if (p2.getHomepage()) {
                img_home = "home.png";
            }
            String img_vis = "unvisibile.png";
            if (p2.getStato().equals("VISIBILE")) {
                img_vis = "visibile.png";
            }


            out.print("<tr id=\"prod_of_part_" + prod_id + "\">");
            out.print("<td>" + p2.getCodice() + "</td>");
            out.print("<td>" + p2.getNome() + "</td>");
            out.print("<td>" + p2.getAnno() + "</td>");
            out.print("<td>" + Util.FormatData(p2.getDataora())  + "</td>");
            out.print("<td><a title=\"Set Un/Visible\" href=\"javascript:visibile(" + prod_id + ");\"><img id=\"img_visibile_" + prod_id + "\" src=\"../engine/img/" + img_vis + "\"></img></a></td>");
            out.print("<td><a title=\"Set to HomePage\" href=\"javascript:homepage(" + prod_id + ");\"><img id=\"img_homepage_" + prod_id + "\" src=\"../engine/img/" + img_home + "\"></img></a></td>");
            out.print("<td><a title=\"Set Promo\" href=\"javascript:promo(" + prod_id + ");\"><img id=\"img_promo_" + prod_id + "\" src=\"../engine/img/" + img_promo + "\"></img></a></td>");
            out.print("<td><a title=\"Delete Product of Part\" href=\"javascript:del_prod_of_part(" + prod_id + "," + part_id + ");\"><img id=\"delete_part_" + prod_id + "\" src=\"../engine/img/delete_on.png\"></img></a></td>");
            out.print("</tr>");
        }
        out.print("</table>");
        
        //!!!IMPORTANTE!!!! rittere la direzione in quella della pagina attuale
        paginator.addParameter("direction", direction);
        out.print(paginator);
        
        em.close();
        //emf.close();
    %>
</div>
<script>
    $("#search").keyup(function () {
        search('search_view_prod.jsp',{pid:<%=request.getParameter("pid")%>});
    });
</script>
<div class="box">
    <div id="search-title" class="title">Search for the products to attach to the part</div>
    <div style="margin-top: 8px;" class="input-search"><label for="search">Code/Name of Products</label><input size="100" name="search" id="search" type="text" value=""/></div>
    <div id="search-result">
        No Result
    </div>
</div>
